package com.ruitu.mall_98_2.mine;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.AfterSaleContract;
import com.ruitu.mall_98_2.mvp.model.AfterSaleModel;
import com.ruitu.mall_98_2.mvp.presenter.AfterSalePresenter;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.order.OrderDetail;
import com.ruitu.router_module.bean.order.SmallOrder;

import java.util.ArrayList;
import java.util.List;

public class AfterSaleActivity extends MyBaseActivity<AfterSalePresenter, AfterSaleModel> implements AfterSaleContract.View {
//    private SmallOrder smallOrder;
    private SmallOrder orderDetail;
    private ImageView iv_commodity;
    private TextView tv_commodity_name;
    private TextView tv_commodity_info;
    private TextView tv_commodity_price;
    private TextView tv_commodity_num;
    private TextView tv_order_money;
    private TextView tv_commodity_status;
    private TextView tv_commit;

    private RelativeLayout rl_select_commodity;
    private EditText et_reason;
    private List<Integer> skip = new ArrayList<>();
    private String commodity_status;
    private String commodity_string;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_after_sale;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("申请售后");
        orderDetail = (SmallOrder) getIntent().getExtras().get("smallOrder");
        skip.add(R.id.header);
        showEmptyView(skip);
        iv_commodity = findViewById(R.id.iv_commodity);
        tv_commodity_name = findViewById(R.id.tv_commodity_name);
        tv_commodity_info = findViewById(R.id.tv_commodity_info);
        tv_commodity_price = findViewById(R.id.tv_commodity_price);
        tv_commodity_num = findViewById(R.id.tv_commodity_num);
        tv_order_money = findViewById(R.id.tv_order_money);
        tv_commodity_status = findViewById(R.id.tv_commodity_status);
        tv_commit = findViewById(R.id.tv_commit);
        rl_select_commodity = findViewById(R.id.rl_select_commodity);
        et_reason = findViewById(R.id.et_reason);
        rl_select_commodity.setOnClickListener(this);
        tv_commit.setOnClickListener(this);
        if (orderDetail != null) {
            Glide.with(this).load(Config.BASE_URL + orderDetail.getGoodsUrl()).apply(Arad.opts).into(iv_commodity);
            tv_commodity_name.setText(orderDetail.getGoodsName());
            tv_commodity_info.setText(orderDetail.getSkuContent());
            tv_commodity_num.setText("x" + orderDetail.getQuantity());
            tv_commodity_price.setText("￥" + orderDetail.getGoodsPrice());
            tv_order_money.setText("￥" + orderDetail.getOrderTotal());
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (rl_select_commodity == v) {
            showCommpdityStatus();
        }

        if (tv_commit == v) {
            if (TextUtils.isEmpty(commodity_status)) {
                ToastUtils.showShort("请选择商品状态");
                return;
            }

            if (TextUtils.isEmpty(et_reason.getText())) {
                ToastUtils.showLong("请填写退款的原因");
                return;
            }
            showProgress();
            mPresenter.afterSaleApply(orderDetail.getId(), commodity_status, et_reason.getText().toString());
        }
    }

    public void showCommpdityStatus() {
        View view = View.inflate(this, R.layout.item_cpmmodity_status, null);
        final PopupWindow window = new PopupWindow(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        ImageView iv_close = view.findViewById(R.id.iv_close);
        TextView tv_not_yet_recived_the_goods = view.findViewById(R.id.tv_not_yet_recived_the_goods);
        TextView tv_yet_recived_the_goods = view.findViewById(R.id.tv_yet_recived_the_goods);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });

        tv_not_yet_recived_the_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commodity_status = "0";
                commodity_string = "未收到货";
                tv_commodity_status.setText(commodity_string);
                window.dismiss();
            }
        });

        tv_yet_recived_the_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commodity_status = "1";
                commodity_string = "已收到货";
                tv_commodity_status.setText(commodity_string);
                window.dismiss();
            }
        });
        window.setContentView(view);
        // 设置PopupWindow是否能响应外部点击事件
        window.setOutsideTouchable(true);
        //设置背景半透明色
        backgroundAlpha(0.5f);
        //popupWindow  事件监听
        window.setOnDismissListener(new PopupDismissListener());
        View rootView = LayoutInflater.from(AfterSaleActivity.this).inflate(R.layout.activity_after_sale, null);
        //将PopupWindow置于activity的底部
        window.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void setSuc() {
        Arad.bus.post(new EventModel(18122000));
        finish();
    }

    class PopupDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }
    }

    private void backgroundAlpha(float v) {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.alpha = v;
        getWindow().setAttributes(layoutParams);
    }
}
